;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.User", [])
        .controller("UserCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter) {
            $scope.activeFlag = [true, false, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
            $scope.roleList = [];
            $scope.dataList = [];
            $scope.detailInfo = {
                sex: 1,
                locked: 0
            };
            $scope.dataSelected = {
                role: {},
                department: {}
            };
            $scope.userStatues = [
                {
                    id: 0,
                    name: "启用"
                }, {
                    id: 1,
                    name: "锁定"
                }
            ];
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };

            $scope.tableConfig = {
                scrollWidth: 1150,
                fixedWidth: 240,
                tableCol: [
                    {
                        type: "",
                        name: "用户账号",
                        dataField: "loginName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "用户名",
                        dataField: "name",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "昵称",
                        dataField: "nickname",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "性别",
                        dataField: "sexShow",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "状态",
                        dataField: "lockedShow",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "联系电话",
                        dataField: "telephone",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "手机号",
                        dataField: "mobile",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "所属部门",
                        dataField: "deptName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "电子邮箱",
                        dataField: "email",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "最后登录时间",
                        dataField: "lastLoginTimeShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 200,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                if (index == 0) {
                    initData();
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {
                    sex: 1,
                    locked: 0
                };
                $scope.roleList = [];
                $scope.dataSelected = {
                    role: {},
                    department: {}
                };
            }

            function getDataList(curPage) {
                var service = "auth";
                var reqUrl = "/profile/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.sexShow = $filter("TrueOrFalse")(item.sex, "sex");
                        item.lockedShow = $filter("TrueOrFalse")(item.locked, "lockStatues");
                        item.lastLoginTimeShow = $filter("YbDate")(item.lastLoginTime, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };

            $scope.addData = function () {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };

            $scope.delData = function (delType, userId) {
                var service = "auth";
                var reqUrl = "/profile/remove";
                var param = {};
                param.userIds = [];

                if (delType === "single") {
                    param.userIds.push(userId);
                }

                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            param.userIds.push($scope.dataList[i].userId);
                        }
                    }
                    if (param.userIds.length === 0) {
                        ybWidget.ShowMessage('请选择需要删除的行');
                        return;
                    }
                }

                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param.userIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };

            $scope.editData = function (user) {
                var service = "auth";
                var reqUrl = "/profile/detail";
                var param = {};
                param.userId = user.userId;

                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1];
                    $scope.changeTab(1);
                    $scope.dataSelected.role.list = resp.value.roleIds;
                    $scope.dataSelected.role.name = resp.value.roleNames;
                    $scope.dataSelected.department.id = resp.value.deptId;
                    $scope.dataSelected.department.departName = resp.value.deptName;
                    $scope.detailInfo = resp.value;
                });
            };

            $scope.restPwd = function (item) {
                var service = "auth";
                var reqUrl = "/account/resetPassword";
                var param = {
                    userIds: []
                };
                param.userIds.push(item.userId);
                ybWidget.ShowMessage("确定将密码重置为123456吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param.userIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                    });
                }, function () {

                });
            };

            $scope.switchLockStatues = function (userId, lockType) {
                var service = "auth";
                var reqUrl = "/profile/updateLock";
                var param = {
                    userIds: []
                };
                param.userIds.push(userId);
                param.lock = 1;
                var tipMsg = "锁定";
                if (lockType === "enabled") {
                    tipMsg = "启用";
                    param.lock = 0;
                }
                ybWidget.ShowMessage("确定" + tipMsg + "该用户吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                }, function () {

                });
            };

            $scope.saveData = function (isValid) {
                if (!isValid || !$scope.dataSelected.role.name) {
                    $scope.submitted = true;
                    return;
                }
                var service = "auth";
                var reqUrl = "/profile";
                var param = {};
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/profile/updateUserInfo";
                    param.userId = $scope.detailInfo.userId;
                }
                param.loginName = $scope.detailInfo.loginName;
                param.name = $scope.detailInfo.name;
                param.nickname = $scope.detailInfo.nickname;
                param.sex = $scope.detailInfo.sex;
                param.deptId = $scope.dataSelected.department.id;
                param.roleIds = $scope.dataSelected.role.list;
                param.telephone = $scope.detailInfo.telephone;
                param.mobile = $scope.detailInfo.mobile;
                param.email = $scope.detailInfo.email;
                param.locked = $scope.detailInfo.locked;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                    $scope.markInfo.allChecked = false;
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);